# -*- coding: utf-8 -*-
import os
import uuid
import tkinter as tk
from tkinter import filedialog
import docx
from os.path import join, getsize

index = 0  # 行数
match_suffix = ['.js','.html']  # 匹配的后缀
fileName = ""
file = docx.Document()
# 写入World文件


def write_to_world(text):
    global index,fileName,file
    if index % 10000 == 0:
        index += 1
        print("已经读取了{0}行，执行写入操作！".format(index))
    try:
        file.add_paragraph(u""+text).italic = True
        file.save(filePath+"\\"+fileName)
        #如果文件大于 3M 重新开辟空间
        if getsize(join(filePath, fileName)) > 3000000:
            file = docx.Document()
            fileName = str(index)+"----"+str(uuid.uuid1())+".docx"
    except:
        print("报错了")

# 读取文件代码


def read_to_js_file(file_path_usr):
    # 全部读取
    with open(file_path_usr,'r', encoding='utf-8', errors='ignore') as f:
        file = f.read()
        arr = file.split("\n")
        print("当前文件行{0}".format(len(arr)))
        for line in arr:
            if str(line).strip() != "":
                write_to_world(line.rstrip())
    # 逐行读取
    # for line in open(file_path_usr,  'r', encoding='utf-8', errors='ignore'):
    #     if str(line).strip() != "":
    #         write_to_world(line.rstrip())

# 获取目录下路径


def get_path_url_usr(path):
    for file in os.listdir(path):
        file_path = os.path.join(path, file)
        if os.path.isdir(file_path):
            get_path_url_usr(file_path)
        else:
            global match_suffix
            suffix = os.path.splitext(file_path)[-1]
            if suffix in match_suffix:
                read_to_js_file(file_path)
                

def get_desk_p():
    return os.path.join(os.path.expanduser('~'),"Desktop")+"\\"

def bytes_to_human(n):
    symbols = ('K','M','G','T','P','E','Z','Y')
    prefix = {}
    for i,s in enumerate(symbols):
        prefix[s] = 1 << (i + 1) * 10
    for s in reversed(symbols):
        if n >= prefix[s]:
            value = float(n) / prefix[s]
            return '%.1f%s' % (value,s)
    return '%sB' % n

if __name__ == '__main__':
    fileName = str(uuid.uuid1())+".docx"
    # print("开始操作。。。。")
    # root=tk.Tk()
    # root.withdraw()
    # filePath=filedialog.askdirectory()
    # print("选择的路径{0}".format(filePath))
    # get_path_url_usr(filePath)
    # input("完活。。。。")
    
    #fileName = str(uuid.uuid1())+".docx"
    #print("开始操作。。。。")
    #filePath=os.getcwd() #选择当前目录中的 文件 包含子集
    #print("选择的路径{0}".format(filePath))
    #get_path_url_usr(filePath)
    #input("完活。。。。")
